package com.woniu.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woniu.common.entity.ResultCode;
import com.woniu.common.exception.MallException;
import com.woniu.common.utils.IdWorker;
import com.woniu.mall.constant.OrderConstants;
import com.woniu.mall.constant.SeckillRedisKey;
import com.woniu.mall.mapper.MallSeckillMapper;
import com.woniu.mall.pojo.MallOrder;
import com.woniu.mall.pojo.MallSeckill;
import com.woniu.mall.service.IMallSeckillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.user.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 秒杀活动表 服务实现类
 * </p>
 *
 * @author Klaus
 * @since 2023-05-27
 */
@Service
public class MallSeckillServiceImpl extends ServiceImpl<MallSeckillMapper, MallSeckill> implements IMallSeckillService {
    @Autowired
    private IMallSeckillService seckillService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void seckill(Integer productId, UserLoginVO userLoginVO) {
        //从redis中获取秒杀的商品
        MallSeckill seckill = (MallSeckill) redisTemplate.boundHashOps(SeckillRedisKey.SECKILL_LIST).get(productId + "");
        if (seckill == null){
            throw new MallException("秒杀的商品不存在", ResultCode.PRODUCT_NOT_EXIST);
        }
        // 判断库存
        if (seckill.getPsCount() <= 0   ){
            throw new MallException("秒杀商品库存不足", ResultCode.PRODUCT_NO_STOCK);
        }
        //创建订单
        MallOrder order = new MallOrder();
        IdWorker idWorker = new IdWorker(1,0);
        //标识优惠券订单
        order.setOrderId((int)idWorker.nextId());
        order.setUserId(userLoginVO.getId());
        order.setTotalPrice(seckill.getPrice());
        //保存订单到redis中
        redisTemplate.boundHashOps(SeckillRedisKey.SECKILL_ORDER).put(userLoginVO.getMobile(), order);
        // 预扣库存
        seckill.setPsCount(seckill.getPsCount() - 1);
        redisTemplate.boundHashOps(SeckillRedisKey.SECKILL_LIST).put(productId + "", seckill);
    }

    @Override
    public void addStartingCoupon(String zoneCouponType, BigDecimal amount, BigDecimal price, Integer quantity, Date startTime, Date endTime) {
        //添加到redis秒杀库存中
        MallSeckill seckillProduct = new MallSeckill();
        IdWorker idWorker = new IdWorker(1,0);
        Integer productId = (int)idWorker.nextId();
        seckillProduct.setProductId(productId);
        seckillProduct.setProductName(OrderConstants.SECKILL_COUPON_NAME_PREFIX + zoneCouponType + amount);
        seckillProduct.setPrice(price);
        seckillProduct.setPsCount(quantity);
        seckillProduct.setStartTime(startTime);
        seckillProduct.setEndTime(endTime);
        redisTemplate.boundHashOps(SeckillRedisKey.SECKILL_LIST).put((int)idWorker.nextId() + "", seckillProduct);

    }

    @Override
    public List<MallSeckill> listStarting() {
        LambdaQueryWrapper<MallSeckill> wrapper = new LambdaQueryWrapper();
        //开始时间小于当前时间
        wrapper.lt(MallSeckill :: getStartTime, new Date());
        //秒杀时间大于当前时间
        wrapper.gt(MallSeckill :: getEndTime, new Date());
        //秒杀状态进行中
        wrapper.eq(MallSeckill :: getStatus, OrderConstants.SECKILL_STATUS_START);
        //商品的秒杀数量大于0
        wrapper.gt(MallSeckill :: getPsCount, 0);
        //调用mapper查询
        List<MallSeckill> list = seckillService.list(wrapper);

        return list;
    }


}
